package com.wrx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;

@SpringBootApplication(exclude = {
    ServletWebServerFactoryAutoConfiguration.class,
    DispatcherServletAutoConfiguration.class
})
public class SwingApplication {
    public static void main(String[] args) {
        // 设置系统属性，确保 Swing 应用在非 headless 模式下运行
        System.setProperty("java.awt.headless", "false");
        
        // 设置文件编码为 UTF-8
        System.setProperty("file.encoding", "UTF-8");
        System.setProperty("sun.jnu.encoding", "UTF-8");
        
        // 设置控制台输出编码（Windows）
        try {
            if (System.getProperty("os.name").toLowerCase().contains("windows")) {
                // 尝试设置控制台编码为 UTF-8
                new ProcessBuilder("cmd", "/c", "chcp", "65001").inheritIO().start().waitFor();
            }
        } catch (Exception e) {
            // 忽略编码设置失败
        }
        
        try {
            SpringApplication app = new SpringApplication(SwingApplication.class);
            // 禁用 Web 服务器启动
            app.setWebApplicationType(org.springframework.boot.WebApplicationType.NONE);
            app.run(args);
            System.out.println("🚀 Swing project started successfully!");
        } catch (Exception e) {
            System.err.println("❌ 启动失败: " + e.getMessage());
            e.printStackTrace();
            // 显示错误对话框（如果可能）
            try {
                javax.swing.JOptionPane.showMessageDialog(
                    null,
                    "应用启动失败:\n" + e.getMessage(),
                    "启动错误",
                    javax.swing.JOptionPane.ERROR_MESSAGE
                );
            } catch (Exception ex) {
                // 如果无法显示对话框，至少打印错误
            }
            System.exit(1);
        }
    }
}